
package cribbage.client;

import com.samskivert.swing.event.CommandEvent;
import com.threerings.crowd.client.PlaceView;
import com.threerings.crowd.data.PlaceObject;
import com.threerings.crowd.util.CrowdContext;
import com.threerings.parlor.card.client.CardGameController;
import com.threerings.parlor.card.data.*;
import com.threerings.parlor.turn.client.TurnGameControllerDelegate;
import com.threerings.presents.dobj.MessageEvent;
import com.threerings.toybox.util.ToyBoxContext;
import com.threerings.util.Name;

import cribbage.data.CribbageCodes;
import cribbage.data.CribbageObject;

import java.awt.event.ActionEvent;

public class CribbageController extends CardGameController implements CribbageCodes {
   private CribbagePanel _panel;

   private CribbageObject _obj;

   private TurnGameControllerDelegate _delegate;

   private Card [] _cribSelected;

   private Card [] _playSelected;

   private int numSelected;

   public CribbageController () {
      addDelegate(_delegate = new TurnGameControllerDelegate(this));
      _cribSelected = new Card [2];
      _playSelected = new Card [1];
      numSelected = 0;
   }

   @Override
   protected PlaceView createPlaceView (CrowdContext ctx) {
      _panel = new CribbagePanel((ToyBoxContext) ctx, this);
      return _panel;
   }

   @SuppressWarnings("unchecked")
   @Override
   public boolean handleAction (ActionEvent action) {
      String cmd = action.getActionCommand();
      if (cmd.equals(PLAY)) {
         if (_obj.turnPhase == MAKE_CRIB) {
            postEvent(PLAY_REQUEST, new Object [] {_cribSelected});
/*
            System.out.print("playing ");
            for (Card c : _cribSelected) {
               System.out.print(c + " ");
            }
*/
         } else if (_obj.turnPhase == NORMAL_PLAY) {
            postEvent(PLAY_REQUEST, new Object [] {_playSelected});
/*
            System.out.print("playing ");
            for (Card c : _playSelected) {
               System.out.print(c + " ");
            }
*/
         }
//         System.out.println("from hand " + _panel.board.myHandArea.getHand());

         _panel.board.playCards();
         anticipateEndTurn();

      } else if (cmd.equals(GO)) {
         postEvent(GO_REQUEST, null);
         anticipateEndTurn();

      } else if (cmd.equals(SELECT)) {
         if (_obj.turnPhase == MAKE_CRIB)
            _cribSelected[numSelected++] = (Card) (((CommandEvent) action).getArgument());
         else
            _playSelected[numSelected++] = (Card) (((CommandEvent) action).getArgument());

         if (_obj.turnPhase == NORMAL_PLAY || (_obj.turnPhase == MAKE_CRIB && numSelected == 2))
            _panel.play.setEnabled(true);

      } else if (cmd.equals(DESELECT)) {
         if (_obj.turnPhase == MAKE_CRIB)
            _cribSelected[--numSelected] = (Card) (((CommandEvent) action).getArgument());
         else
            _playSelected[--numSelected] = (Card) (((CommandEvent) action).getArgument());
         _panel.play.setEnabled(false);

      } else if (cmd.equals(BACK_TO_LOBBY)) {
         _ctx.getLocationDirector().moveBack();

      } else if (cmd.equals(PLAYER_GAME_OVER)) {
         _ctx.getLocationDirector().moveBack();

      } else {
         return super.handleAction(action);
      }
      return true;
   }

   private void postEvent (String event, Object [] args) {
      _ctx.getDObjectManager().postEvent(new MessageEvent(_obj.getOid(), event, args));
   }

   private void anticipateEndTurn () {
      _panel.play.setEnabled(false);
   }

   @Override
   public void turnDidChange (Name turnHolder) {
      numSelected = 0;
      _cribSelected[0] = _cribSelected[1] = _playSelected[0] = null;

      _panel.board.turnDidChange(turnHolder, _delegate.isOurTurn());
      _panel.play.setEnabled(false);
   }

   @Override
   public void willEnterPlace (PlaceObject plobj) {
      super.willEnterPlace(plobj);
      _obj = (CribbageObject) plobj;
   }

   @Override
   public void receivedHand (Hand hand) {
      _panel.board.receivedHand(hand);
   }

   @Override
   protected void gameDidStart () {
      super.gameDidStart();
      _panel.board.gameDidStart();
   }
}
